using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TCClientService;

namespace TimeCameraWinForm
{
  public partial class Login : Form
  {
    //static volatile bool close = false;//用于停止死循环
    //List<Account> list;//测试
    System.Net.Http.HttpClient Client;
    Task loginTask;
    //Logininformation infor = new Logininformation();
    public Login(System.Net.Http.HttpClient client)
    {
      InitializeComponent();
      //list = new List<Account>();
      registerAccount.Enabled = false;
      Client = client;
      showGif_Behind(".//pic//3.gif");
    }
    private void login_button_Click(object sender, EventArgs e)
    {
      loginTask=TCClientService.AccountService.LoginAsync(Client, new TCEntity.User(account_Box.Text, passport_Box.Text),
      loginSuc(),loginFail(),netFail());
      showGif_Front(".//pic//1.gif");
      //showGif_Front(".//pic//1.gif",close);
      this.Close();
    }

    /*private void login_button_Click(object sender, EventArgs e)
    {
        if (account_Box.Text != string.Empty && passport_Box.Text != string.Empty)
        {
            Account user = new Account(account_Box.Text, passport_Box.Text);
            foreach (Account i in list)
            {
                if (user.match(i))
                {
                    Logininformation a = new Logininformation();
                    a.Login_infor.Text = "登录成功";//显示的信息
                    a.ShowDialog();
                    //显示主页面
                   return;
                }
            }
        }
        else
        {
            Logininformation a = new Logininformation();
            a.Login_infor.Text = "账号或密码不能为空";//显示的信息
            a.ShowDialog();
            //相关提示
        }

    }*/
    private void register_button_Click(object sender, EventArgs e)
    {

      
      login_button.Visible = false;//按键处理
      register_button.Visible = false;
      forget_button.Visible = false;
      login_button.Enabled = false;
      register_button.Enabled = false;
      forget_button.Enabled = false;
      registerAccount.Enabled = true;
      registerAccount.Visible = true;
      registerAccount.Text = "注册账号";
      //注册页面
    }//dont change


    private void forget_button_Click(object sender, EventArgs e)
  {
    //忘记密码页面
  }
  /*public void showGif(string gif, bool close)
  {
    this.Show();
    Bitmap animatedGif = new Bitmap(gif);
    Graphics g = this.panel1.CreateGraphics();
    // A Gif image's frame delays are contained in a byte array
    // in the image's PropertyTagFrameDelay Property Item's
    // value property.
    // Retrieve the byte array...
    int PropertyTagFrameDelay = 0x5100;
    PropertyItem propItem = animatedGif.GetPropertyItem(PropertyTagFrameDelay);
    byte[] bytes = propItem.Value;
    // Get the frame count for the Gif...
    FrameDimension frameDimension = new FrameDimension(animatedGif.FrameDimensionsList[0]);
    int frameCount = animatedGif.GetFrameCount(FrameDimension.Time);
    // Create an array of integers to contain the delays,
    // in hundredths of a second, between each frame in the Gif image.
    int[] delays = new int[frameCount + 1];
    int i = 0;
    for (i = 0; i <= frameCount - 1; i++)
    {
      delays[i] = BitConverter.ToInt32(bytes, i * 4);
    }

    // Play the Gif one time...
    while (!close)
    {
      for (i = 0; i <= animatedGif.GetFrameCount(frameDimension) - 1; i++)
      {
        animatedGif.SelectActiveFrame(frameDimension, i);
        g.DrawImage(animatedGif, new Point(0, 0));
        Application.DoEvents();
        Thread.Sleep(delays[i] * 10);
      }
    }


  }*/
  private void registerAccount_Click(object sender, EventArgs e)
    {
      //close = false;
      //Thread thread = new Thread(() =>
      //  TCClientService.AccountService.RegisterAsync(Client, new TCEntity.User(account_Box.Text, passport_Box.Text),
      //  registerSuc(), registerFail(), netFail()));
      //thread.Start();
      loginTask=TCClientService.AccountService.RegisterAsync(Client, new TCEntity.User(account_Box.Text, passport_Box.Text),
      registerSuc(), registerFail(), netFail());
      showGif_Front(".//pic//1.gif");
      login_button.Visible = true;
      register_button.Visible = true;
      forget_button.Visible = true;
      login_button.Enabled = true;
      register_button.Enabled = true;
      forget_button.Enabled = true;
      registerAccount.Visible = false;
      registerAccount.Enabled = false;
      account_Box.Text = null;
      passport_Box.Text = null;
  }


    private Action loginSuc()
    {
      return new Action(() =>
      {
        MessageBox.Show("登录成功");
        Hide();
        new MainForm(new TCEntity.User(account_Box.Text, passport_Box.Text), Client).ShowDialog(this);
        showGif_Behind(".//pic//3.gif");//恢复到登录页面
        //this.Close();//登录窗口自动关闭
      });
    }
    private Action<String> loginFail()
    {
      return new Action<string>((s) =>
      {
        MessageBox.Show("登录失败");
        showGif_Behind(".//pic//3.gif");//恢复到登录页面
        //close = true;
      });
    }
    private Action netFail()
    {
      return new Action(() =>
      {
        
        MessageBox.Show("网络错误");
        showGif_Behind(".//pic//3.gif");//恢复到登录页面
                                     
      });
    }
    private Action registerSuc()
    {
      return new Action(() =>
      {
        MessageBox.Show("注册成功");
        login_button.Visible = true;//按键处理
        register_button.Visible = true;
        forget_button.Visible = true;
        login_button.Enabled = true;
        register_button.Enabled = true;
        forget_button.Enabled = true;
        registerAccount.Enabled = false;
        registerAccount.Visible = false;
        showGif_Behind(".//pic//3.gif");//恢复到登录页面
        //close = true;
        //thread.Abort();//调用Thread.Abort方法试图强制终止thread线程  
        //while (thread.ThreadState != ThreadState.Aborted)
        //{
        //  //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止  
        //  Thread.Sleep(100);
        //}

      });
    }
    private Action<String> registerFail()
    {
      return new Action<string>((s) =>
      {
        MessageBox.Show("注册失败");
        showGif_Behind(".//pic//3.gif");//恢复到登录页面
        //close = true; 
      });
    }
    //public static void reload(System.Net.Http.HttpClient client)
    //{
    //  Login a = new Login(client);
    //  a.Show();
    //}
    public void showGif_Behind(string gif)
    {
      panel1.SendToBack();
      this.Show();
      Bitmap animatedGif = new Bitmap(gif);
      Graphics g = this.panel1.CreateGraphics();
      // A Gif image's frame delays are contained in a byte array
      // in the image's PropertyTagFrameDelay Property Item's
      // value property.
      // Retrieve the byte array...
      int PropertyTagFrameDelay = 0x5100;
      PropertyItem propItem = animatedGif.GetPropertyItem(PropertyTagFrameDelay);
      byte[] bytes = propItem.Value;
      // Get the frame count for the Gif...
      FrameDimension frameDimension = new FrameDimension(animatedGif.FrameDimensionsList[0]);
      int frameCount = animatedGif.GetFrameCount(FrameDimension.Time);
      // Create an array of integers to contain the delays,
      // in hundredths of a second, between each frame in the Gif image.
      int[] delays = new int[frameCount + 1];
      int i = 0;
      for (i = 0; i <= frameCount - 1; i++)
      {
        delays[i] = BitConverter.ToInt32(bytes, i * 4);
      }

      // Play the Gif one time...
      while (true)
      {
        for (i = 0; i <= animatedGif.GetFrameCount(frameDimension) - 1; i++)
        {
          animatedGif.SelectActiveFrame(frameDimension, i);
          g.DrawImage(animatedGif, new Point(0, 0));
          Application.DoEvents();
          Thread.Sleep(delays[i] * 10);
        }
      }
    }
    public void showGif_Front(string gif)//以秒为单位
    {
      panel1.BringToFront();
      this.Show();
      Bitmap animatedGif = new Bitmap(gif);
      Graphics g = this.panel1.CreateGraphics();
      // A Gif image's frame delays are contained in a byte array
      // in the image's PropertyTagFrameDelay Property Item's
      // value property.
      // Retrieve the byte array...
      int PropertyTagFrameDelay = 0x5100;
      PropertyItem propItem = animatedGif.GetPropertyItem(PropertyTagFrameDelay);
      byte[] bytes = propItem.Value;
      // Get the frame count for the Gif...
      FrameDimension frameDimension = new FrameDimension(animatedGif.FrameDimensionsList[0]);
      int frameCount = animatedGif.GetFrameCount(FrameDimension.Time);
      // Create an array of integers to contain the delays,
      // in hundredths of a second, between each frame in the Gif image.
      int[] delays = new int[frameCount + 1];
      int i = 0;
      for (i = 0; i <= frameCount - 1; i++)
      {
        delays[i] = BitConverter.ToInt32(bytes, i * 4);
      }// Play the Gif one time...
      //Stopwatch s = new Stopwatch();
      //s.Start();
      while (!loginTask.IsCompleted)
      {
        for (i = 0; i <= animatedGif.GetFrameCount(frameDimension) - 1; i++)
        {
          animatedGif.SelectActiveFrame(frameDimension, i);
          g.DrawImage(animatedGif, new Point(0, 0));
          Application.DoEvents();
          Thread.Sleep(delays[i] * 10);
        }
      }
    }
  }

}
